package com.spring.cloud.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.spring.cloud.ribbon.controller.ConsumerController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/7/13.
 */

@Service
public class HelloService {

    private  final Logger logger = LoggerFactory.getLogger(HelloService.class);

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloFallback")
    public String helloService(){

        long startTime = System.currentTimeMillis();

        String result = restTemplate.getForEntity("http://USER-SERVICE/hello", String.class).getBody();

        long endTime = System.currentTimeMillis();

        logger.info("sleepTime: {}", endTime-startTime);

        return result;
    }

    public String helloFallback(){
        return "error";
    }
}
